package com.example.tellwin.home.adpater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tellwin.R;
import com.example.tellwin.mine.bean.EvaluateBean;
import com.example.tellwin.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import per.wsj.library.AndRatingBar;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {

    Context context;
    private List<EvaluateBean> evaluateBeans = new ArrayList<>();

    public void setEvalutateBeans(List<EvaluateBean> evalutateBeans) {
        this.evaluateBeans = evaluateBeans;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new CommentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
        EvaluateBean evaluateBean = evaluateBeans.get(position);
        GlideUtils.loadHeadImage(context, evaluateBean.getHeadPic(), holder.commenterHeadIv);
        holder.commenterNameTv.setText(evaluateBean.getRealName());
        holder.commentTimeTv.setText(evaluateBean.getCreateTime());
        holder.commenterContentTv.setText(evaluateBean.getContent());
        holder.rb.setRating(Integer.valueOf(evaluateBean.getScore()));
    }

    @Override
    public int getItemCount() {
        return evaluateBeans.size();
    }

    class CommentViewHolder extends RecyclerView.ViewHolder {
        ImageView commenterHeadIv;
        TextView commenterNameTv;
        TextView commentTimeTv;
        TextView commenterContentTv;
        AndRatingBar rb;

        public CommentViewHolder(@NonNull View itemView) {
            super(itemView);
            commenterHeadIv = (ImageView) itemView.findViewById(R.id.commenter_head_iv);
            commenterNameTv = (TextView) itemView.findViewById(R.id.commenter_name_tv);
            commentTimeTv = (TextView) itemView.findViewById(R.id.comment_time_tv);
            commenterContentTv = (TextView) itemView.findViewById(R.id.commenter_content_tv);
            rb = itemView.findViewById(R.id.comment_rb);
        }
    }
}
